package Program.UserUI;

import Program.Information.User;
import Program.LoginUI.LoginUI;
import Program.Tool.PictureAlterJPanel;
import Program.Tool.TabbedPaneDef;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;

public class UserUI extends JFrame {
    //用户界面的选项卡
    private JTabbedPane uTabbedPane1, uTabbedPane2, uTabbedPane3, uTabbedPane4;
    //顶端容器
    private JPanel northPanel;
    //按钮容器
    private JPanel JButtonPanel;
    //最小化按钮
    private JButton minButton;
    //关闭按钮
    private JButton closeButton;
    //更换用户按钮
    private JButton changeButton;
    //费用界面按钮
    private JButton feeButton;
    //报修界面按钮
    private JButton repairButton;
    //投诉界面按钮
    private JButton complaintButton;
    //用户信息查询按钮
    private JButton lookUserButton;
    //查看费用单据选项
    private JPanel lookFeeJPanel;
    //查看历史账单选项
    private JPanel hBillingJPanel;
    //查看当前用户类型的收费标准选项
    private JPanel chargesJPanel;
    //费用界面
    private FeePaneSon1 feePaneSon1;
    private FeePaneSon2 feePaneSon2;
    private FeePaneSon3 feePaneSon3;
    //报修查看和删除选项
    private JPanel repairJPanel;
    //报修添加选项
    private JPanel addRepairJPanel;
    //报修界面
    private RepairPaneSon1 repairPaneSon1;
    private RepairPaneSon2 repairPaneSon2;
    //投诉查看和删除选项
    private JPanel complaintJPanel;
    //投诉添加选项
    private JPanel addComplaintJPanel;
    //投诉界面
    private ComplaintPaneSon1 complaintPaneSon1;
    private ComplaintPaneSon2 complaintPaneSon2;
    //用户信息选项
    private JPanel userJPanel;
    //用户信息显示界面
    private UserPaneSon userPaneSon;
    //充值预存款选项
    private JPanel rechargeJPanel;
    //充值预存款显示界面
    private RechargePaneSon rechargePaneSon;
    //用户
    private User user;
    //设置颜色
    Color color = new Color(191, 230, 240);
    //设置字体
    private Font font = new Font("微软雅黑", Font.BOLD, 20);
    //记录鼠标x轴上的位置
    private int mouseAtX = 0;
    //记录鼠标y轴上的位置
    private int mouseAtY = 0;
    //临时按钮
    private JButton tempJButton;
    //获取时间对象
    private Calendar instance = Calendar.getInstance();


    public UserUI(User user) throws HeadlessException {
        this.user = user;
        //界面初始化
        initJFrame();
        //获取按钮监听器
        ActionListener acListener = getAcListener();
        //添加监听器
        closeButton.addActionListener(acListener);
        minButton.addActionListener(acListener);
        changeButton.addActionListener(acListener);
        feeButton.addActionListener(acListener);
        repairButton.addActionListener(acListener);
        complaintButton.addActionListener(acListener);
        lookUserButton.addActionListener(acListener);
        this.tempJButton = feeButton;
        canDraged(northPanel);
        //让界面显示出来，建议写在最后
        this.setVisible(true);
    }

    //主界面
    private void initJFrame() {
        //设置窗口标题图标
        this.setIconImage(new ImageIcon("Application_images\\用户界面图标.png").getImage());
        //设置窗口没又修饰框
        this.setUndecorated(true);
        //设置窗口的大小
        this.setSize(900, 800);
        //设置窗口禁止用户调整
        this.setResizable(false);
        //设置窗口居中
        this.setLocationRelativeTo(null);
        //设置窗口没有布局
        this.setLayout(null);
        //设置窗口背景颜色
        this.getContentPane().setBackground(color);
        //设置窗口关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //加载界面
        initNorthPanel();
        initJButtonPanel();
        this.add(initFeePanel());
    }

    private void initNorthPanel() {
        northPanel = new PictureAlterJPanel("Application_images\\headPane.png");
        northPanel.setBounds(0, 0, 900, 60);
        northPanel.setBackground(Color.white);

        //标题标签
        JLabel showJLabel = new JLabel();
        //设置标签位置
        showJLabel.setBounds(20, 0, 600, 60);
        //设置标签字体
        showJLabel.setFont(font);
        String str = "物业管理系统-居民模式  H1！" + this.user.getName() + "  今天是 " + getDateStr();
        //设置内容
        showJLabel.setText(str);
        //添加控件
        northPanel.add(showJLabel);

        //按钮赋予的对象
        minButton = new JButton();
        closeButton = new JButton();
        changeButton = new JButton();

        //设置按钮的位置
        minButton.setBounds(786, 5, 50, 50);
        closeButton.setBounds(845, 5, 50, 50);
        changeButton.setBounds(727, 5, 50, 50);
        //取消按钮的焦点框
        minButton.setFocusPainted(false);
        closeButton.setFocusPainted(false);
        changeButton.setFocusPainted(false);
        //取消按钮的边框
        minButton.setBorder(null);
        closeButton.setBorder(null);
        changeButton.setBorder(null);
        //设置按钮背景透明
        minButton.setContentAreaFilled(false);
        closeButton.setContentAreaFilled(false);
        changeButton.setContentAreaFilled(false);
        //设置鼠标样式
        minButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        closeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        changeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //设置按钮不同状态时的图标
        //设置按钮图标
        minButton.setIcon(new ImageIcon("Application_images\\最小化按钮1.png"));  //未选中时
        minButton.setRolloverIcon(new ImageIcon("Application_images\\最小化按钮2.png"));  //选中时
        minButton.setPressedIcon(new ImageIcon("Application_images\\最小化按钮3.png"));  //按下时

        closeButton.setIcon(new ImageIcon("Application_images\\关闭按钮1.png"));  //未选中时
        closeButton.setRolloverIcon(new ImageIcon("Application_images\\关闭按钮2.png"));  //选中时
        closeButton.setPressedIcon(new ImageIcon("Application_images\\关闭按钮3.png"));   //按下时

        changeButton.setIcon(new ImageIcon("Application_images\\change1.png"));
        changeButton.setRolloverIcon(new ImageIcon("Application_images\\change2.png"));
        changeButton.setPressedIcon(new ImageIcon("Application_images\\change3.png"));


        //添加控件
        northPanel.add(changeButton);
        northPanel.add(minButton);
        northPanel.add(closeButton);


        //添加面板
        this.add(northPanel);
    }

    //中间按钮界面
    private void initJButtonPanel() {
        //设置界面格式
        JButtonPanel = new JPanel(null);
        JButtonPanel.setBackground(Color.white);
        JButtonPanel.setBounds(0, 65, 900, 100);
        JButtonPanel.setBorder(BorderFactory.createLineBorder(new Color(127, 255, 212), 3));


        //按钮格式

        //费用
        //费用标签
        JLabel feeJLabel = new JLabel("费  用");
        //设置字体
        feeJLabel.setFont(font);
        //设置位置和大小
        feeJLabel.setBounds(152, 60, 65, 50);
        //费用按钮
        feeButton = new JButton();
        //取消按钮的焦点框
        feeButton.setFocusPainted(false);
        //取消按钮的边框
        feeButton.setBorder(null);
        //设置按钮背景透明
        feeButton.setContentAreaFilled(false);
        //设置鼠标样式
        feeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));  //未选中时
        feeButton.setRolloverIcon(new ImageIcon("Application_images\\费用界面按钮2.png"));  //选中时
        //设置位置和大小
        feeButton.setBounds(150, 5, 64, 64);
        //添加控件
        JButtonPanel.add(feeJLabel);
        JButtonPanel.add(feeButton);


        //报修
        //报修标签
        JLabel repairJLabel = new JLabel("报  修");
        //设置字体
        repairJLabel.setFont(font);
        //设置位置和大小
        repairJLabel.setBounds(320, 60, 65, 50);
        //报修按钮
        repairButton = new JButton();
        //取消按钮的焦点框
        repairButton.setFocusPainted(false);
        //取消按钮的边框
        repairButton.setBorder(null);
        //设置按钮背景透明
        repairButton.setContentAreaFilled(false);
        //设置鼠标样式
        repairButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));  //未选中时
        repairButton.setRolloverIcon(new ImageIcon("Application_images\\报修按钮2.png"));  //选中时
        //设置位置和大小
        repairButton.setBounds(315, 5, 64, 64);
        //添加控件
        JButtonPanel.add(repairJLabel);
        JButtonPanel.add(repairButton);

        //投诉
        //投诉标签
        JLabel complaintJLabel = new JLabel("投  诉");
        //设置字体
        complaintJLabel.setFont(font);
        //设置位置和大小
        complaintJLabel.setBounds(485, 60, 65, 50);
        //投诉按钮
        complaintButton = new JButton();
        //取消按钮的焦点框
        complaintButton.setFocusPainted(false);
        //取消按钮的边框
        complaintButton.setBorder(null);
        //设置按钮背景透明
        complaintButton.setContentAreaFilled(false);
        //设置鼠标样式
        complaintButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));  //未选中时
        complaintButton.setRolloverIcon(new ImageIcon("Application_images\\投诉按钮2.png"));  //选中时
        //设置位置和大小
        complaintButton.setBounds(480, 5, 64, 64);
        //添加控件
        JButtonPanel.add(complaintJLabel);
        JButtonPanel.add(complaintButton);


        //查看用户信息
        //查看用户信息标签
        JLabel luJLabel = new JLabel("用户信息");
        //设置字体
        luJLabel.setFont(font);
        //设置位置和大小
        luJLabel.setBounds(647, 60, 80, 50);
        //查看用户信息按钮
        lookUserButton = new JButton();
        //取消按钮的焦点框
        lookUserButton.setFocusPainted(false);
        //取消按钮的边框
        lookUserButton.setBorder(null);
        //设置按钮背景透明
        lookUserButton.setContentAreaFilled(false);
        //设置鼠标样式
        lookUserButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        lookUserButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));  //未选中时
        lookUserButton.setRolloverIcon(new ImageIcon("Application_images\\用户信息查询按钮2.png"));  //选中时
        //设置位置和大小
        lookUserButton.setBounds(655, 5, 64, 64);
        //添加控件
        JButtonPanel.add(luJLabel);
        JButtonPanel.add(lookUserButton);

        this.add(JButtonPanel);
    }

    //底部费用内容界面
    private JTabbedPane initFeePanel() {
        //选项卡赋予对象
        uTabbedPane1 = new JTabbedPane();
        //容器赋予对象
        lookFeeJPanel = new JPanel(null);
        hBillingJPanel = new JPanel(null);
        chargesJPanel = new JPanel(null);

        //容器设置背景颜色
        lookFeeJPanel.setBackground(color);
        hBillingJPanel.setBackground(color);
        chargesJPanel.setBackground(color);

        //添加选项卡容器，并且设置其中每个选项卡的标签以及其是否可启用
        uTabbedPane1.addTab("查询缴费", lookFeeJPanel);
        uTabbedPane1.setEnabledAt(0, true);
        uTabbedPane1.addTab("历史账单", hBillingJPanel);
        uTabbedPane1.setEnabledAt(1, true);
        uTabbedPane1.addTab("收费标准", chargesJPanel);
        uTabbedPane1.setEnabledAt(2, true);

        uTabbedPane1.setUI(new TabbedPaneDef());
        uTabbedPane1.setBounds(0, 162, 900, 700);
        uTabbedPane1.setFont(font);
        uTabbedPane1.setForeground(Color.white);

        feePaneSon1 = new FeePaneSon1(user, getYear(), getMonth());
        lookFeeJPanel.add(feePaneSon1);
        feePaneSon2 = new FeePaneSon2(user);
        hBillingJPanel.add(feePaneSon2);
        feePaneSon3 = new FeePaneSon3(user);
        chargesJPanel.add(feePaneSon3);

        uTabbedPane1.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
                int selectedIndex = sourceTabbedPane.getSelectedIndex();
                if (selectedIndex == 0) {
                    lookFeeJPanel.removeAll();
                    lookFeeJPanel.repaint();
                    feePaneSon1 = new FeePaneSon1(user, getYear(), getMonth());
                    lookFeeJPanel.add(feePaneSon1);
                } else if (selectedIndex == 1) {
                    hBillingJPanel.removeAll();
                    hBillingJPanel.repaint();
                    feePaneSon2 = new FeePaneSon2(user);
                    hBillingJPanel.add(feePaneSon2);
                } else if (selectedIndex == 2) {
                    chargesJPanel.removeAll();
                    chargesJPanel.repaint();
                    feePaneSon3 = new FeePaneSon3(user);
                    chargesJPanel.add(feePaneSon3);
                }
            }
        });

        return uTabbedPane1;
    }

    //底部报修内容界面
    private JTabbedPane initRepairPanel() {
        //选项卡赋予对象
        uTabbedPane2 = new JTabbedPane();
        //容器赋予对象
        repairJPanel = new JPanel(null);
        addRepairJPanel = new JPanel(null);

        //容器设置背景颜色
        repairJPanel.setBackground(color);
        addRepairJPanel.setBackground(color);

        //添加选项卡容器，并且设置其中每个选项卡的标签以及其是否可启用
        uTabbedPane2.addTab("查询报修", repairJPanel);
        uTabbedPane2.setEnabledAt(0, true);
        uTabbedPane2.addTab("添加报修", addRepairJPanel);
        uTabbedPane2.setEnabledAt(1, true);

        uTabbedPane2.setUI(new TabbedPaneDef());
        uTabbedPane2.setBounds(0, 162, 900, 700);
        uTabbedPane2.setFont(font);
        uTabbedPane2.setForeground(Color.white);

        repairPaneSon1 = new RepairPaneSon1(user);
        repairJPanel.add(repairPaneSon1);
        repairPaneSon2 = new RepairPaneSon2(user, getDateStr());
        addRepairJPanel.add(repairPaneSon2);

        uTabbedPane2.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
                int selectedIndex = sourceTabbedPane.getSelectedIndex();
                if (selectedIndex == 0) {
                    repairJPanel.removeAll();
                    repairJPanel.repaint();
                    repairPaneSon1 = new RepairPaneSon1(user);
                    repairJPanel.add(repairPaneSon1);
                } else if (selectedIndex == 1) {
                    addRepairJPanel.removeAll();
                    addRepairJPanel.repaint();
                    repairPaneSon2 = new RepairPaneSon2(user, getDateStr());
                    addRepairJPanel.add(repairPaneSon2);
                }
            }
        });
        return uTabbedPane2;
    }

    //底部投诉内容界面
    private JTabbedPane initComplaintPanel() {
        //选项卡赋予对象
        uTabbedPane3 = new JTabbedPane();
        //容器赋予对象
        complaintJPanel = new JPanel(null);
        addComplaintJPanel = new JPanel(null);

        //容器设置背景颜色
        complaintJPanel.setBackground(color);
        addComplaintJPanel.setBackground(color);

        //添加选项卡容器，并且设置其中每个选项卡的标签以及其是否可启用
        uTabbedPane3.addTab("查询投诉", complaintJPanel);
        uTabbedPane3.setEnabledAt(0, true);
        uTabbedPane3.addTab("添加投诉", addComplaintJPanel);
        uTabbedPane3.setEnabledAt(1, true);

        uTabbedPane3.setUI(new TabbedPaneDef());
        uTabbedPane3.setBounds(0, 162, 900, 700);
        uTabbedPane3.setFont(font);
        uTabbedPane3.setForeground(Color.white);

        complaintPaneSon1 = new ComplaintPaneSon1(user);
        complaintJPanel.add(complaintPaneSon1);
        complaintPaneSon2 = new ComplaintPaneSon2(user, getDateStr());
        addComplaintJPanel.add(complaintPaneSon2);

        uTabbedPane3.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
                int selectedIndex = sourceTabbedPane.getSelectedIndex();
                if (selectedIndex == 0) {
                    complaintJPanel.removeAll();
                    complaintJPanel.repaint();
                    complaintPaneSon1 = new ComplaintPaneSon1(user);
                    complaintJPanel.add(complaintPaneSon1);
                } else if (selectedIndex == 1) {
                    addComplaintJPanel.removeAll();
                    addComplaintJPanel.repaint();
                    complaintPaneSon2 = new ComplaintPaneSon2(user, getDateStr());
                    addComplaintJPanel.add(complaintPaneSon2);
                }
            }
        });
        return uTabbedPane3;
    }

    //底部用户信息界面
    private JTabbedPane initUserJPanel() {
        //选项卡赋予对象
        uTabbedPane4 = new JTabbedPane();
        //容器赋予对象
        userJPanel = new JPanel(null);
        rechargeJPanel = new JPanel(null);
        //容器设置背景颜色
        userJPanel.setBackground(color);
        rechargeJPanel.setBackground(color);
        //添加选项卡容器，并且设置其中每个选项卡的标签以及其是否可启用
        uTabbedPane4.addTab("查询信息", userJPanel);
        uTabbedPane4.setEnabledAt(0, true);
        uTabbedPane4.addTab("充值存款", rechargeJPanel);
        uTabbedPane4.setEnabledAt(1, true);

        uTabbedPane4.setUI(new TabbedPaneDef());
        uTabbedPane4.setBounds(0, 162, 900, 700);
        uTabbedPane4.setFont(font);
        uTabbedPane4.setForeground(Color.white);

        userPaneSon = new UserPaneSon(user);
        userJPanel.add(userPaneSon);
        rechargePaneSon = new RechargePaneSon(user);
        rechargeJPanel.add(rechargePaneSon);

        uTabbedPane4.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
                int selectedIndex = sourceTabbedPane.getSelectedIndex();
                if (selectedIndex == 0) {
                    userJPanel.removeAll();
                    userJPanel.repaint();
                    userPaneSon = new UserPaneSon(user);
                    userJPanel.add(userPaneSon);
                } else if (selectedIndex == 1) {
                    rechargeJPanel.removeAll();
                    rechargeJPanel.repaint();
                    rechargePaneSon = new RechargePaneSon(user);
                    rechargeJPanel.add(rechargePaneSon);
                }
            }
        });
        return uTabbedPane4;
    }

    //获取时间
    private String getDateStr() {
        //3.获取属性
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
        int day = instance.get(Calendar.DAY_OF_MONTH);

        return year + "/" + month + "/" + day;
    }

    //获取年
    private int getYear() {
        int year = instance.get(Calendar.YEAR);
        return year;
    }

    //获取月
    private int getMonth() {
        int month = instance.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
        return month;
    }

    //获取日
    private int getDay() {
        int day = instance.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    //设置窗口移动
    public void canDraged(JPanel p) {
        //创建接收鼠标事件的监听器
        MouseAdapter mouseAdapter = new MouseAdapter() {
            //当鼠标按下时获取窗口的坐标位置
            public void mousePressed(MouseEvent e) {
                mouseAtX = e.getPoint().x;
                mouseAtY = e.getPoint().y;
            }
        };
        MouseMotionAdapter motionAdapter = new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                //设置获取拖拽后的位置并窗口的位置
                setLocation((e.getXOnScreen() - mouseAtX), (e.getYOnScreen() - mouseAtY));
            }
        };
        //给顶部容器添加监听事件
        p.addMouseListener(mouseAdapter);
        p.addMouseMotionListener(motionAdapter);
    }

    public ActionListener getAcListener() {
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == closeButton) {
                    System.exit(0);
                } else if (e.getSource() == minButton) {
                    setExtendedState(JFrame.ICONIFIED);
                } else if (e.getSource() == changeButton) {
                    SwingUtilities.invokeLater(() -> new LoginUI());
                    dispose();
                } else if (e.getSource() == feeButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮3.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));
                    lookUserButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));
                    delJPanel();
                    add(initFeePanel());
                    tempJButton = feeButton;
                } else if (e.getSource() == repairButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮3.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));
                    lookUserButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));
                    delJPanel();
                    add(initRepairPanel());
                    tempJButton = repairButton;
                } else if (e.getSource() == complaintButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮3.png"));
                    lookUserButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));
                    delJPanel();
                    add(initComplaintPanel());
                    tempJButton = complaintButton;
                } else if (e.getSource() == lookUserButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));
                    lookUserButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮3.png"));
                    delJPanel();
                    add(initUserJPanel());
                    tempJButton = lookUserButton;
                }
            }
        };
        return listener;
    }

    private void delJPanel() {
        if (tempJButton == feeButton) {
            remove(uTabbedPane1);
            repaint();
        } else if (tempJButton == repairButton) {
            remove(uTabbedPane2);
            repaint();
        } else if (tempJButton == complaintButton) {
            remove(uTabbedPane3);
            repaint();
        } else if (tempJButton == lookUserButton) {
            remove(uTabbedPane4);
            repaint();
        }
    }
}
